﻿using System;
using HttpApiGateway.Interface;
using RpcCacheClient.Interface;
using RpcClient.Interface;
using RpcHelper;

namespace HttpApiGateway.Idempotent.Key
{
    [RpcClient.Attr.UnityName("Cache")]
    [RpcClient.Attr.ClassLifetimeAttr(RpcClient.Attr.ClassLifetimeType.单例)]
    internal class CacheKeyService : IKeyIdempotent
    {
        private readonly ICacheController _Cache;
        private readonly IIdempotentConfig _Config;
        public CacheKeyService(ICacheController cache, IIdempotentConfig config)
        {
            _Cache = cache;
            _Config = config;
        }
        public StatusSaveType SaveType => StatusSaveType.Cache;
        public void Dispose()
        {

        }
       
        public bool SubmitToken(string tokenId)
        {
            string key = "Idem_" + tokenId;
            return _Cache.Add(key, tokenId, new TimeSpan(0, 0, _Config.Expire));
        }
    }
}
